21. Quiz: Parse JSON

Quiz: Parse JSON

Write the function to return the condition.

QUESTION:

Given the following JSON, write a function to retrieve the weather "condition".

{
   "temp": {
      "min":"11.34",
      "max":"19.01"
   },
   "weather": {
      "id":"801",
      "condition":"Clouds",
      "description":"few clouds"
   },
   "pressure":"1023.51",
   "humidity":"87"
}
ANSWER:

Here is the answer:

String getCondition(String jsonString) {
   JSONObject forecast = new JSONObject(jsonString);
   JSONObject weather = forecast.getJSONObject("weather");
   return weather.getString("condition");
}